package com.zhongy.util.util.classloader;



import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


/**
 * 
 *
 *PluginManager
 *主要功能用于控制热加载的jar包和里面的实现类
 *注意点：当前项目中不能有Class路径一样的类，不然会优先加载项目中的类不能实现动态加载
 *zhongy
 *2015年1月9日-上午8:55:19
 *@version 1.0.0
 */


public class PluginManager {

	private static String jarDir="";//jar包的目录路径
	private static String packageAndClassPath = "";//类的具体路径
		
	public  Map<String, PluginClassLoader> pluginMap = new HashMap<String, PluginClassLoader>();
	// 用来存放加载那个类的加载器
	// key=account_id
	// value=loader
	public PluginManager(){};
	/**
	 * 此构造器在系统中只能初始化一次
	 * @param jarDir 存放jar包的目录
	 * @param packageAndClassPath jar包中类的完整地址
	 */
	private PluginManager(String jarDir,String packageAndClassPath){
		this.jarDir=jarDir;
		
		this.packageAndClassPath=packageAndClassPath;
		
	};
//	static {
//		System.out.println(PluginManager.class.getName());
//	}


	
	private static PluginManager manager;
	 public static PluginManager getInstance(String jarDir,String packageAndClassPath){
	        if(manager==null){
	        	manager=new PluginManager(jarDir,packageAndClassPath);
	        }
	        return manager;
	    }   
	

	private void addLoader(String pluginName, PluginClassLoader loader) {
		this.pluginMap.put(pluginName, loader);
	}

	private PluginClassLoader getLoader(String pluginName) {
		return this.pluginMap.get(pluginName);
	}
	
	

	/**
	 * 
	 *@描述	:在manager对象初始化以后用来载入jar包
	 *@创建人：zhongy
	 *@创建时间：2015年2月10日 上午10:48:36
	 *@修改人：
	 *@修改时间：
	 *@修改描述：
	 *@param
	 *@return
	 *
	 * @param account_id account_id 用来区分多个jar包的key
	 * @param jarName jar包的文件名称
	 */
	public void loadPlugin(String account_id,String jarName) {
		this.pluginMap.remove(account_id);
		PluginClassLoader loader = new PluginClassLoader();
		String pluginurl = "jar:file:/" + jarDir + jarName + "!/";
		System.out.println(pluginurl);
		URL url = null;
		try {
			url = new URL(pluginurl);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loader.addURLFile(url);
		addLoader(account_id, loader);
		System.out.println("load " + account_id + "  success");
	}
	/**
	 *  方法功能：在manager对象初始化以后用来卸载jar包
	 * @param account_id 用来区分多个jar包的key
	 */
	public void unloadPlugin(String account_id) {
		this.pluginMap.get(account_id).unloadJarFiles();
		this.pluginMap.remove(account_id);
	}

	/**
	 * 方法作用：通过key来获取抽象实体类
	 * @param account_id 用来区分多个jar包的key
	 * @return 返回抽象实体类
	 */
	public Class<?> getClass(String account_id){
		try {
			Class<?> forName = Class.forName(packageAndClassPath, true,getLoader(account_id));// this.pluginMap.get(pluginName).loadClass(packagename);
			return forName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	};
	
	
	
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException {

		String jarDir="D:/hotLib/";
		String packageAndClassPath = "com.nowwel.supply.Trade";
		PluginManager manager=PluginManager.getInstance(jarDir,packageAndClassPath);
		manager.loadPlugin("1", "1.jar");//载入jar包
//		BaseTradeInterface ins=(BaseTradeInterface)(manager.getClass(String.valueOf(1)).newInstance());//初始化抽象类
//		ins.queryStatus(null); 
	}

}